package book.manage;

import book.manage.dao.IInputDao;
import book.manage.po.Student;
import book.manage.sql.SqlUtil;
import lombok.extern.java.Log;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import book.manage.po.Book;

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.LogManager;

@Log
public class main {
    public static void main(String[] args) throws IOException {
        LogManager manager = LogManager.getLogManager();
        manager.readConfiguration(Resources.getResourceAsStream("JUL/logging.properties"));

        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("========================");
            System.out.println("1.录入学生信息");
            System.out.println("2.录入书籍信息");
            System.out.println("3.添加借阅信息");
            System.out.println("4.查询借阅列表");
            System.out.println("5.查询学生借阅列表");
            System.out.print("输入执行的操作：");
            int n;
            try{
                n = scanner.nextInt();
            }catch (Exception e){
                return;
            }
            scanner.nextLine();//读取回车
            switch (n){
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    addBook(scanner);
                    break;
                case 3:
                    addBorrow(scanner);
                    break;
                case 4:
                    getborrowlist();
                    break;
                case 5:
                    getstudentborrowlist(scanner);
                    break;
                default:
                    return;
            }


        }
    }

    private static void getstudentborrowlist(Scanner scanner)
    {
        System.out.print("请输入sid：");
        int i = scanner.nextInt();
        scanner.nextLine();
        SqlUtil.doSqlWork(mapper->{
            System.out.println(mapper.getStudentBorrowList(i));
            log.info("查询一次借阅信息");
        });
    }

    private static void getborrowlist()
    {
        SqlUtil.doSqlWork(mapper->{
            System.out.println(mapper.getBorrowList());
            log.info("查询一次借阅信息");
        });
    }

    private static void addBorrow(Scanner scanner){
        System.out.print("请输入sid：");
        int sid = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入bid：");
        int bid = scanner.nextInt();
        scanner.nextLine();
        SqlUtil.doSqlWork(mapper -> {
            int i = mapper.addBorrow(sid, bid);
            if(i>0) {
                System.out.println("录入成功");
                log.info("录入一条借阅信息");
            }
        });
    }

    private static void addStudent(Scanner scanner){
        System.out.print("请输入学生name：");
        String name = scanner.nextLine();
        System.out.print("请输入学生sex：");
        String sex = scanner.nextLine();

        System.out.print("请输入学生grade：");
        int grade = scanner.nextInt();
        scanner.nextLine();

        Student student = new Student(name, sex, grade);
        SqlUtil.doSqlWork(mapper -> {
            int i = mapper.addStudent(student);
            if(i>0) {
                System.out.println("录入成功");
                log.info("录入一条学生信息");
            }
        });
    }

    private static void addBook(Scanner scanner){
        System.out.print("请输入书籍title：");
        String title = scanner.nextLine();
        System.out.print("请输入书籍desc：");
        String desc = scanner.nextLine();

        System.out.print("请输入书籍price：");
        double price = scanner.nextDouble();
        scanner.nextLine();

        Book book = new Book(title, desc, price);
        SqlUtil.doSqlWork(mapper -> {
            int i = mapper.addBook(book);
            if(i>0) {
                System.out.println("录入成功");
                log.info("录入一条书籍信息");
            }
        });
    }
}
